package com.shenmazg6.recv;

import com.alibaba.fastjson2.JSON;
import com.rabbitmq.client.Channel;
import com.shenmazg6.pojo.TbOrder;
import com.shenmazg6.service.TbOrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * @author 军哥
 * @version 1.0
 * @description: ReceiveMessage
 * @date 2022/12/20 11:55
 */

@Component
@Slf4j
public class ReceiveMessage {

    @Autowired
    TbOrderService tbOrderService;

    @RabbitListener(queues = "dead-orderout")
    public void handleMessage(Message message, Channel channel) throws IOException {

        // 获取消息内容
        String body = new String(message.getBody());
        log.warn("body:"+body);

        // 检查是否重复消费
        // TODO

        // 取消订单
        TbOrder tbOrder = JSON.parseObject(body, TbOrder.class);
        tbOrderService.cancelOrder(tbOrder);

        // 手动确认消息
        channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
    }
}
